package com.tuju.jetpackfirstdemo.ui.texttospeech;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.speech.tts.TextToSpeech;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.tuju.jetpackfirstdemo.R;

import java.util.Locale;

public class SpeechActivity extends AppCompatActivity implements View.OnClickListener {

    private EditText et_input;
    private Button btn_play;
    private Button btn_pause;
    private Button btn_release;

    private TextToSpeech textToSpeech;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_speech);
        initView();
        initTTS();
    }

    private void initView() {
        et_input = findViewById(R.id.et_input);
        btn_play = findViewById(R.id.btn_play);
        btn_pause = findViewById(R.id.btn_pause);
        btn_release = findViewById(R.id.btn_release);
        findViewById(R.id.btn_clear).setOnClickListener(this);
        btn_play.setOnClickListener(this);
        btn_pause.setOnClickListener(this);
        btn_release.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_clear:
                et_input.setText("");
                break;
            case R.id.btn_play:
                String data = et_input.getText().toString().trim();
                startSpeech(data);
                break;
            case R.id.btn_pause:
                stopSpeech();
                break;
            case R.id.btn_release:
                shutDownSpeech();
                break;
        }
    }

    private void initTTS() {
        //实例化自带语音对象
        textToSpeech = new TextToSpeech(this, new TextToSpeech.OnInitListener() {
            @Override
            public void onInit(int status) {
                if (status == textToSpeech.SUCCESS) {
                    // Toast.makeText(MainActivity.this,"成功输出语音",
                    // Toast.LENGTH_SHORT).show();
                    // Locale loc1=new Locale("us");
                    // Locale loc2=new Locale("china");

                    textToSpeech.setPitch(1.0f);//方法用来控制音调
                    textToSpeech.setSpeechRate(1.0f);//用来控制语速

                    //判断是否支持下面两种语言
                    int result1 = textToSpeech.setLanguage(Locale.US);
                    int result2 = textToSpeech.setLanguage(Locale.SIMPLIFIED_CHINESE);
                    boolean a = (result1 == TextToSpeech.LANG_MISSING_DATA || result1 == TextToSpeech.LANG_NOT_SUPPORTED);
                    boolean b = (result2 == TextToSpeech.LANG_MISSING_DATA || result2 == TextToSpeech.LANG_NOT_SUPPORTED);

                    Log.i("yanze_speech", "US支持否？--》" + a +
                            "\nzh-CN支持否》--》" + b);
                } else {
                    Log.i("yanze_speech", "数据丢失或不支持");
                }
            }
        });
    }

    private void stopSpeech() {
        if (textToSpeech != null) {
            textToSpeech.stop(); // 不管是否正在朗读TTS都被打断
        }
    }

    private void shutDownSpeech() {
        if (textToSpeech != null) {
            textToSpeech.shutdown();//释放资源
            textToSpeech = null;
        }
    }

    private void startSpeech(String data) {
        Log.i("yanze_speech", "startSpeech: " + data);
        if (textToSpeech != null) {
            // 设置音调，值越大声音越尖（女生），值越小则变成男声,1.0是常规
            textToSpeech.setPitch(1.0f);
            // 设置语速
            textToSpeech.setSpeechRate(2.0f);
            textToSpeech.speak(data,//输入中文，若不支持的设备则不会读出来
                    TextToSpeech.QUEUE_ADD, null);
        }
    }

}
